package org.spring.springboot.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.spring.springboot.entity.InitiateInvitation;
import org.spring.springboot.entity.InviteTeg;
import org.spring.springboot.po.InitiateInvitationPO;
import org.spring.springboot.result.R;
import org.spring.springboot.result.Result;
import org.spring.springboot.server.InitiateInvitationService;
import org.spring.springboot.util.ConstantUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "发起邀约")
@RequestMapping("/initiateInvitation")
public class InitiateInvitationController {
    @Autowired
    private InitiateInvitationService initiateInvitationService;

    @ApiOperation(value = "新增邀约")
    @PostMapping("/addInvite")
    public Result addIntervie(@RequestBody InitiateInvitationPO initiateInvitationPO){
        initiateInvitationService.addInvite(initiateInvitationPO);
        return Result.newSuccessResult(initiateInvitationPO);
    }

    @ApiOperation(value = "新增邀约同行")
    @PostMapping("/addInviteTeg")
    public Result addInviteTeg(@RequestBody InviteTeg inviteTeg){
        int id = initiateInvitationService.selectId(inviteTeg);
        inviteTeg.setFid(id);
        initiateInvitationService.insertInviteTeg(inviteTeg);
        return Result.newSuccessResult(inviteTeg);
    }

    @PostMapping(value = "/inviteNameList")
    @ApiOperation("查询被邀约人姓名")
    public Result InviteNameList(@RequestParam("id") Integer id){
        List<Map<String, Object>> maps = initiateInvitationService.getUserNameList(id);
        return Result.newSuccessResult(maps);
    }

    @ApiOperation(value = "邀约完成")
    @PostMapping("/updateCompleteId")
    public Result updateCompleteId(@RequestParam(value = "id") Integer id,
                                   @RequestParam(value = "completeId") Integer completeId){
        initiateInvitationService.updateComplete(id, completeId);
        return Result.newSuccessResult("id:"+id+" completeId:"+completeId);
    }


    @ApiOperation("查看邀约历史记录")
    @PostMapping("/HistoryRecord")
    public R HistoryRecord(@RequestParam("starttime") Timestamp starttime,
                                                   @RequestParam("endtime") Timestamp endtime,
                                                   @RequestParam("pageNum") Integer pageNum,
                                                   @RequestParam("pageSzie") Integer pageSize){
        Page page = PageHelper.startPage(pageNum, pageSize);
        List<Map<String, Object>> maps = initiateInvitationService.selectHistort(starttime, endtime);
        Long total = page.getTotal();
        return R.newSuccessResult(maps,total);
    }

}
